package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrEnum;
import org.road0329.dlt698.asn1.axdr.AxdrType;

public class GetRequest extends AxdrType {
    private byte[] dataCode = null;
    private Choice choice = Choice._ERR_NONE_SELECTED;
    private GetRequestNormal getRequestNormal = null;
    private GetRequestNormalList getRequestNormalList = null;
    private GetRequestRecord getRequestRecord = null;
    private GetRequestRecordList getRequestRecordList = null;
    private GetRequestNext getRequestNext = null;

    public GetRequest() {
    }

    public void set_dataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setGetRequestNormal(GetRequestNormal getRequestNormal) {
        resetChoices();
        this.choice = Choice.GET_REQUEST_NORMAL;
        this.getRequestNormal = getRequestNormal;
    }

    public void setGetRequestNormalList(GetRequestNormalList getRequestNormalList) {
        resetChoices();
        this.choice = Choice.GET_REQUEST_NORMAL_LIST;
        this.getRequestNormalList = getRequestNormalList;
    }

    public void setGetRequestRecord(GetRequestRecord getRequestRecord) {
        resetChoices();
        this.choice = Choice.GET_REQUEST_RECORD;
        this.getRequestRecord = getRequestRecord;
    }

    public void setGetRequestRecordList(GetRequestRecordList getRequestRecordList) {
        resetChoices();
        this.choice = Choice.GET_REQUEST_RECORD_LIST;
        this.getRequestRecordList = getRequestRecordList;
    }

    public void setGetRequestNext(GetRequestNext getRequestNext) {
        resetChoices();
        this.choice = Choice.GET_REQUEST_NEXT;
        this.getRequestNext = getRequestNext;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        AxdrEnum choosen = new AxdrEnum();
        choosen.setConst();
        codeLength += choosen.decode(input);
        resetChoices();
        this.choice = Choice.getEnumByValue((int)choosen.getValue());
        if (this.choice == Choice.GET_REQUEST_NORMAL) {
            this.getRequestNormal = new GetRequestNormal();
            codeLength += this.getRequestNormal.decode(input);
            return codeLength;
        } else if (this.choice == Choice.GET_REQUEST_NORMAL_LIST) {
            this.getRequestNormalList = new GetRequestNormalList();
            codeLength += this.getRequestNormalList.decode(input);
            return codeLength;
        } else if (this.choice == Choice.GET_REQUEST_RECORD) {
            this.getRequestRecord = new GetRequestRecord();
            codeLength += this.getRequestRecord.decode(input);
            return codeLength;
        } else if (this.choice == Choice.GET_REQUEST_RECORD_LIST) {
            this.getRequestRecordList = new GetRequestRecordList();
            codeLength += this.getRequestRecordList.decode(input);
            return codeLength;
        } else if (this.choice == Choice.GET_REQUEST_NEXT) {
            this.getRequestNext = new GetRequestNext();
            codeLength += this.getRequestNext.decode(input);
            return codeLength;
        }
        throw new Exception("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        if (this.dataCode != null) {
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
            return this.dataCode.length;
        }
        if (this.choice == Choice._ERR_NONE_SELECTED) {
            throw new Exception("Error encoding AxdrChoice: No item in choice was selected.");
        }
        int codeLength = 0;
        if (this.choice == Choice.GET_REQUEST_NORMAL) {
            if (this.getRequestNormal != null) {
                codeLength += this.getRequestNormal.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(1);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("getRequestNormal is null");
            }
        } else if (this.choice == Choice.GET_REQUEST_NORMAL_LIST) {
            if (this.getRequestNormalList != null) {
                codeLength += this.getRequestNormalList.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(2);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("getRequestNormalList is null");
            }
        } else if (this.choice == Choice.GET_REQUEST_RECORD) {
            if (this.getRequestRecord != null) {
                codeLength += this.getRequestRecord.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(3);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("getRequestRecord is null");
            }
        } else if (this.choice == Choice.GET_REQUEST_RECORD_LIST) {
            if (this.getRequestRecordList != null) {
                codeLength += this.getRequestRecordList.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(4);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("getRequestRecordList is null");
            }
        } else if (this.choice == Choice.GET_REQUEST_NEXT) {
            if (this.getRequestNext != null) {
                codeLength += this.getRequestNext.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(5);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("getRequestNext is null");
            }
        }
        throw new Exception("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    public Choice getChoiceIndex() {
        return this.choice;
    }

    public void resetChoices() {
        this.choice = Choice._ERR_NONE_SELECTED;
        this.getRequestNormal = null;
        this.getRequestNormalList = null;
        this.getRequestRecord = null;
        this.getRequestRecordList = null;
        this.getRequestNext = null;
    }

    public String toString() {
        if (this.choice == Choice.GET_REQUEST_NORMAL) {
            if (this.getRequestNormal != null) {
                return "choice: {getRequestNormal: " + this.getRequestNormal + "}";
            } else {
                return "choice is getRequestNormal but getRequestNormal is null";
            }
        } else if (this.choice == Choice.GET_REQUEST_NORMAL_LIST) {
            if (this.getRequestNormalList != null) {
                return "choice: {getRequestNormalList: " + this.getRequestNormalList + "}";
            } else {
                return "choice is getRequestNormalList but getRequestNormalList is null";
            }
        } else if (this.choice == Choice.GET_REQUEST_RECORD) {
            if (this.getRequestRecord != null) {
                return "choice: {getRequestRecord: " + this.getRequestRecord + "}";
            } else {
                return "choice is getRequestRecord but getRequestRecord is null";
            }
        } else if (this.choice == Choice.GET_REQUEST_RECORD_LIST) {
            if (this.getRequestRecordList != null) {
                return "choice: {getRequestRecordList: " + this.getRequestRecordList + "}";
            } else {
                return "choice is getRequestRecordList but getRequestRecordList is null";
            }
        } else if (this.choice == Choice.GET_REQUEST_NEXT) {
            if (this.getRequestNext != null) {
                return "choice: {getRequestNext: " + this.getRequestNext + "}";
            } else {
                return "choice is getRequestNext but getRequestNext is null";
            }
        } else {
            return "unknown";
        }
    }

    public enum Choice {
        _ERR_NONE_SELECTED(-1),
        GET_REQUEST_NORMAL(1),
        GET_REQUEST_NORMAL_LIST(2),
        GET_REQUEST_RECORD(3),
        GET_REQUEST_RECORD_LIST(4),
        GET_REQUEST_NEXT(5);

        private final int value;

        private Choice(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static Choice getEnumByValue(int value) {
            for (Choice item : Choice.values()) {
                if (value == item.getValue()) {
                    return item;
                }
            }
            return null;
        }
    }
}


